/**
 * @(#)PoolObjetoListaInstancias.java Apr 3, 2007
 *
 * Copyright (c) 2005-2010 Psiqueware S.A. de C.V. All Rights Reserved.
 * This software is the proprietary information Psiqueware S.A. de C.V.
 * Use is subject to license terms.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * <br/>Usuario : AngelElectrico
 * <br/>Creaci&oacute;n: Apr 3, 2007 - 4:31:54 PM
 * <br/>
 * <br/>
 * <br/><t5>Cambios :</t5>
 * <br/><b>Fecha | Autor  |  Cambios</b>
 * <ul>
 * <li>Apr 3, 2007 | <a href = "mailto:AngelElectrico@PsiqueWare.com">Angel El&eacute;ctrico</a> | Creaci&oacute;n del Objeto.</li>
 *
 * </ul>
 */

package com.psiqueware.alheli.reflection.pooling;

import com.psiqueware.alheli.reflection.RefleccionClases;

import java.util.ArrayList;
import java.util.List;

/**
 * Pool para objetos que requieran ciertas garantias de disponibilidad.
 * Un pool se implementa para garantizar la disponibilidad de clases que ejecuten una
 * tarea dada.
 * <br/>
 * Es importante notar que esta clase solo funciona para objetos que tienen un constructor
 * sin argumentos.
 * <br/>Paquete Original: com.psiqueware.basico.manager
 * <br/>Nombre  Original: PoolObjetoListaInstancias
 * <br/>Oprima para ver : <a href="PoolObjetoListaInstancias.java.html"><i>C&oacute;digo Fuente</i> </a>
 *
 * @author <a href="mailto:AngelElectrico@PsiqueWare.com">Angel El&eacute;ctrico</a>
 * @version Revision: 1.1 Apr 3, 2007 4:31:54 PM
 */
public final class PoolObjetoListaInstancias<T> implements PoolObjeto
{

    private static final int MAXIMO_POOL = 5;

    /**
     * Arreglo con los manager.
     */
    final List<T> objetosPooleados = new ArrayList<T>(MAXIMO_POOL);

    /**
     * Indice del &uacute;ltimo objeto obtenido de la lista.
     */
    private int m_indiceActual = 0;

    /**
     * Clase que implementa va a ser cacheada.
     */
    private final Class m_clazz;


    /**
     * Constructor de PoolObjetoListaInstancias.
     */
    public PoolObjetoListaInstancias(Class clazz)
    {
        m_clazz = clazz;
    }

    /**
     * Devuelve un Objeto de tipo T.
     *
     * @return instancia de un objeto de tipo T.
     */
    public T referencia()
    {

        if (m_indiceActual >= MAXIMO_POOL) m_indiceActual = 0;
        while (objetosPooleados.size() <= m_indiceActual)
        {
            final T o = (T) RefleccionClases.nuevaInstancia(m_clazz);
            objetosPooleados.add(o);
        }
        final T objetoNuevo = objetosPooleados.get(Math.min(m_indiceActual, objetosPooleados.size() - 1));
        m_indiceActual++;
        return objetoNuevo;
    }


}

